package GUI;

import model.*;
import GUI.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import controller.Client;


public class MainFrame extends JFrame {
	
	private JMenuBar menuBar;
	private JMenu gameMenu, helpMenu, profileMenu;
	private JMenuItem about, logOut, returnToLobby, yourprofile;
	private JSplitPane splitPane;
	
	private final Player player;
	private final Map map;
	private Enemy[] enemyList;
	private MapPanel mapPanel;
	private TextPanel textPanel;
	
	public MainFrame(Player _player, Map _map, Enemy[] _enemyList) {//for test
		super("Pair GUI");
		player = _player;
		map = _map;
		enemyList = _enemyList;
		textPanel = new TextPanel(player, map, enemyList);
		mapPanel = new MapPanel(player, map);
		textPanel.setMapPanel(mapPanel);
		mapPanel.setTextPanel(textPanel);
		initComponents();
		setSize(1350, 700);
		setVisible(true);
	}

	private void initComponents() {
		getContentPane().setLayout(new BorderLayout());
		createMenuBar();
		createSplitPane();
	}
	
	private void createMenuBar() {
		menuBar = new JMenuBar();
		gameMenu = new JMenu("Game");
		menuBar.add(gameMenu);
		
		logOut = new JMenuItem(new AbstractAction("Log Out") {
			public void actionPerformed(ActionEvent ae) {
	           //return to login, David
				int n = JOptionPane.showOptionDialog(this,
					    "Are you sure you want to log out?",
					  "Warning",
					   JOptionPane.YES_NO_CANCEL_OPTION,
					    JOptionPane.QUESTION_MESSAGE
					    );
			}
		});
		
		returnToLobby = new JMenuItem(new AbstractAction("Return to Lobby") {
			public void actionPerformed(ActionEvent ae) {
				//actually return to lobby, David
				int n = JOptionPane.showOptionDialog(this,
				    "Are you sure you want to return to lobby?",
				    "Warning",
				    JOptionPane.YES_NO_CANCEL_OPTION,
				    JOptionPane.QUESTION_MESSAGE
				    );
			}
		});
		
		gameMenu.add(returnToLobby);
		gameMenu.add(logOut);
		
		profileMenu = new JMenu("Profile");
		menuBar.add(profileMenu);
		
		yourprofile = new JMenuItem(new AbstractAction("Your Profile") {
			public void actionPerformed(ActionEvent ae) {
				JOptionPane.showMessageDialog(null, "You didn't have anything here yet.");
			}
		});
		
		profileMenu.add(yourprofile);
		
		helpMenu = new JMenu("Help");
		menuBar.add(helpMenu);
		
		about = new JMenuItem(new AbstractAction("About") {
			public void actionPerformed(ActionEvent ae) {
				JOptionPane.showMessageDialog(null, "Pair GUI made on 2/23/14 as an example");
			}
		});
		
		helpMenu.add(about);
		setJMenuBar(menuBar);
		
	}
	
	private void createSplitPane() {
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, mapPanel, textPanel);
		splitPane.setDividerLocation(700);
		splitPane.setOneTouchExpandable(true);
		add(splitPane);
	}
	
	private void setupGame() {
		//Setup Connection to the actual game server
	}
	
	public void newTurn(){
		textPanel.updateBeforeTurn();
		player.feed();
	}
	
}